/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pradoeditor.comun;

import java.util.Date;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;

/**
 *
 * @author jaime
 */
public class SendMail extends Thread
{
    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final String SMTP_PORT = "465";
    private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

    private String to = "jaimea.gaviria@gmail.com";
    private String from = "jaimea.gaviria@gmail.com";
    private String subject;
    private String body;

    public SendMail (String to, String from, String subject, String body)
    {
        this.to = to;
        this.from = from;
        this.subject = subject;
        this.body = body;
    }

    public SendMail (String subject, String body)
    {
        this.subject = subject;
        this.body = body;
    }

    public void sendMail(String to, String from, String subject, String body)
    {       
        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "false");
        props.put("mail.smtp.port", SMTP_PORT);
        props.put("mail.smtp.socketFactory.port", SMTP_PORT);
        props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.put("mail.smtp.socketFactory.fallback", "false");

        Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator()
        {
            @Override
            protected PasswordAuthentication getPasswordAuthentication()
            {
                return new PasswordAuthentication("jaimea.gaviria@gmail.com", "jimmy0203+");
            }
        });

        session.setDebug(false);

        try
        {
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] address = {new InternetAddress(to)};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject(subject);
            msg.setSentDate(new Date());

            MimeBodyPart mbp1 = new MimeBodyPart();
            mbp1.setText(body);

            Multipart mp = new MimeMultipart();
            mp.addBodyPart(mbp1);
            msg.setContent(mp);

            Transport.send(msg);
        }
        catch (MessagingException e)
        {
          //  JOptionPane.showMessageDialog(null, e.toString());
        }
    }

    public void sendMail(String subject, String body)
    {
        sendMail(to, from, subject, body);
    }

    public void run()
    {
        sendMail(to, from, subject, body);
    }

}
